package cqrtplm.aop;

import com.ty.basic.entity.PrincipalEntity;
import com.ty.basic.entity.org.OrgDO;
import com.ty.basic.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Aspect
@Component
@Slf4j
public class RTGetParentOrgAop {

    /**
     * 创建表单时，给表单中的 DocumentNumberForm 字段赋值
     * @param proceedingJoinPoint
     * @return
     * @throws Throwable
     */
    @Around("execution(* com.hustcad.plm.pdm.user.controller.TyplmTeamController.getParentOrg(..))")
    public Object createFormDataWithFile(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object proceed = proceedingJoinPoint.proceed();
        ResponseResult<List<PrincipalEntity>> outParent = (ResponseResult<List<PrincipalEntity>>) proceed;
        List<PrincipalEntity> data = outParent.getData();
        List<PrincipalEntity> data1 = new ArrayList<>();
        boolean flag = false;
        for(PrincipalEntity outParentEntity : data){
            OrgDO orgDO = (OrgDO) outParentEntity;
            String parentPath = orgDO.getParentPath();
            if(StringUtils.isNotBlank(parentPath)){
                flag = true;
                data1.add(outParentEntity);
            }
        }
        if(!flag){
            for(PrincipalEntity outParentEntity : data){
                OrgDO orgDO = (OrgDO) outParentEntity;
                String displayFullName = orgDO.getDisplayFullName();
                if(!"润通科技".equals(displayFullName)){
                    data1.add(outParentEntity);
                }
            }
        }
        outParent.setData(data1);
        return outParent;
    }

}
